package com.jwdf.uda.ui.fragment.search;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentSearchResultsBinding;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.DisplayUtils;

/**
 * Created by Administrator on 2016/9/18.
 */
public class SearchDesignerFragment extends BaseFragment {
    private static final int SEARCH_ALL = 0;
    private static final int SEARCH_FOCUS = 1;
    private static final int TAB_COUNT = 2;
    private String mContent;
    private int posSel=0;

    private FragmentSearchResultsBinding mDataBinding;


    public static SearchDesignerFragment instance(String content) {
        SearchDesignerFragment view = new SearchDesignerFragment();
        Bundle args = new Bundle();
        args.putString("content", content);
        view.setArguments(args);
        return view;
    }

    private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if(posSel != position) {
                final TextView txt = mDataBinding.tabs.getTxt(position);

                final int size = DisplayUtils.px2sp(getContext(), txt.getTextSize());
                final int unitSP = TypedValue.COMPLEX_UNIT_SP;
                mDataBinding.tabs.getTxt(posSel).setTextSize(unitSP, size);
                txt.setTextSize(unitSP, size + 1);

                posSel = position;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDataBinding =DataBindingUtil.inflate(inflater, R.layout.fragment_search_results, container, false);
        initViews();
        return mDataBinding.getRoot();
    }

    private void initViews() {
        mDataBinding.vpM.setOffscreenPageLimit(TAB_COUNT);
        FragmentAdapter adapter = new FragmentAdapter(getActivity().getSupportFragmentManager());
        mDataBinding.vpM.setAdapter(new FragmentAdapter(getActivity().getSupportFragmentManager()));
        adapter.notifyDataSetChanged();
        mDataBinding.tabs.setViewPager(mDataBinding.vpM);
        mDataBinding.tabs.setOnPageChangeListener(mOnPageChangeListener);

        mDataBinding.vpM.setCurrentItem(0);

        final TextView txt = mDataBinding.tabs.getTxt(posSel);

        final int size = DisplayUtils.px2sp(getContext(), txt.getTextSize());
        final int unitSP = TypedValue.COMPLEX_UNIT_SP;
        txt.setTextSize(unitSP, size + 1);

//        String userId = ConfigUtils.getUserId(getContext());
//        ArrayList<String> records = ConfigUtils.restoreStringArray(getContext(), userId,false);
//        records.add(mContent);
//        ConfigUtils.saveStringArray(getContext(), userId, records);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EventBusUtil.registerEvent(this);
        mContent = getArguments().getString("content", "");
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
//        EventBusUtil.unregisterEvent(this);

    }
    public class FragmentAdapter extends FragmentStatePagerAdapter {

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position >= 0 && position < TAB_COUNT) {
                switch (position) {
                    case SEARCH_ALL:
                        return SearchDesignerResultsFragment.instance(mContent, PbBean.PbSignSearchType.SEARCH_TYPE_DESIGNER);
                    case SEARCH_FOCUS:
                        return SearchDesignerResultsFragment.instance(mContent, PbBean.PbSignSearchType.SEARCH_TYPE_FOLLOW_DESIGNER);
                    default:
                        break;
                }
            }
            return null;
        }

        @Override
        public int getCount() {
            return TAB_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position >= 0 && position < TAB_COUNT) {
                switch (position) {
                    case SEARCH_ALL:
                        return "所有";
                    case SEARCH_FOCUS:
                        return "我关注的";
                    default:
                        break;
                }
            }
            return null;
        }
    }

}
